<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 属性控制器
 * @author huajie <banhuajie@163.com>
 */
class GoodsController extends AdminController {

    /**
     * 属性列表
     * @author huajie <banhuajie@163.com>
     */
    public function index(){
        $model_id = I('get.model_id');
        /* 查询条件初始化 */
        $map['model_id']    =   $model_id;

        $list = $this->lists('Attribute', $map);
        int_to_string($list);

        // 记录当前列表页的cookie
        Cookie('__forward__',       $_SERVER['REQUEST_URI']);
        $this->assign('_list',      $list);
        $this->assign('model_id',   $model_id);
        $this->meta_title = '属性列表';
        $this->display();
      
    }

    
    
    /**
     * 分类列表
     * @author hexin<332050842@qq.com>
     */
    public function classlist(){
    	//         $pid = i('get.pid', 0);
    	//         /* 获取频道列表 */
    	//         $map  = array('status' => array('gt', -1), 'pid'=>$pid);
    	//         $list = M('Channel')->where($map)->order('sort asc,id asc')->select();
    	//         $this->assign('list', $list);
    	//         $this->assign('pid', $pid);
    	
    	$list = M('goodscat')->order('sort asc,id asc')->select();
    	$this->assign('aadd', "ccss");
    	$this->assign('list', $list);
    	
    	$this->meta_title = '商家管理';
    	$this->display();
    }
    
    /**
     * 添加商家
     * @author hexin<332050842@qq.com>
     */
    public function goodsadd(){
   
        //获取商家信息列表
        $shoplist      =   M('Goodshops')->where('state=1')->field('id,shopname')->order('sort')->select();
        //获取分类信息
        $cate      =   M('Goodscat')->where('state=1')->field('id,cat_name')->order('sort')->select();
       // file_put_contents("g:/mylog.log","tttt"."\r\n",FILE_APPEND);
        //获取分类信息
        $this->assign('catlist', $cate);
        $this->assign('shoplist', $shoplist);
    	$this->display('goodsadd');
    	
    	 
    }
    
    /**
     * 增加商品
     * @author huajie <banhuajie@163.com>
     */
    public function insertone(){
       
        //file_put_contents("g:/mylog.log","aa123"."\r\n",FILE_APPEND);
       // $good_name=$_POST['good_name'];
        
        //       /* /*   //解析规格  并出入数据库
        //         $ruleall=$_POST['ruleall'];
        //         file_put_contents("g:/mylog.log",$ruleall."\r\n",FILE_APPEND);
        //         $de_json = json_decode($ruleall,TRUE);
        //         $count_json = count($de_json);
                
        //        //将规格循环插入数据库中
        //        for ($i = 0; $i < $count_json; $i++)
        //        {
        //     file_put_contents("g:/mylog.log","==ddd123=".$de_json[$i]['ruleprice']."\r\n",FILE_APPEND);
        //     $Goodsrule = D('Goodsrule');
        //     $data['good_id'] = 11;
        //     $data['rule_name'] = $de_json[$i]['rulename'];
        //     $data['rule_price'] = $de_json[$i]['ruleprice'];
        //     $data['stock'] = $de_json[$i]['rulestock'];
        //     $Goodsrule->add($data);
            
        //        } */ */
        $Goods=D('Goods');
        $data = $Goods->create();
        file_put_contents("g:/mylog.log","=ggg==".$data['cat_id']."\r\n",FILE_APPEND);
        
        if($data){
            $id = $Goods->add();
            if($id){        
            				$this->success('新增成功', U('classlist'));
            				//记录行为
            				action_log('update_config', 'goodscat', $id, UID);
            } else {
            				$this->error('新增失败');
            }
        } else {
            $this->error($GoodsCate->getError());
        }

   
    }
    /**
     * 添加商家
     * @author hexin<332050842@qq.com>
     */
    public function classadd(){
        file_put_contents("g:/mylog.log","=1111=="."\r\n",FILE_APPEND);
    	if(IS_POST){
    		$GoodsCate = D('goodscat');
    		
    		
    		
    		
    		$data = $GoodsCate->create();
    		file_put_contents("g:/mylog.log","===".$data."\r\n",FILE_APPEND);
    		if($data){
    			$id = $GoodsCate->add();
    			if($id){
//     				//插入到目录中
//     				$Menu = D('Menu');
//     				$Mdata['title'] = $data['cat_name'];
//     				$Mdata['pid'] = '124';
//     				$Mdata['sort'] = $data['sort'];
//     				$Mdata['url'] = 'Goods/classlist/'.$id.'.html';
//     				$Mdata['group'] = '商品管理';
//     				$Menu->add($Mdata);
    				
    				$this->success('新增成功', U('classlist'));
    				//记录行为
    				action_log('update_config', 'goodscat', $id, UID);
    			} else {
    				$this->error('新增失败');
    			}
    		} else {
    			$this->error($GoodsCate->getError());
    		}
    	} else {

    		$this->assign('info',null);
    		$this->meta_title = '新增导航';
    		$this->display('classedit');
    	}
    	
    }
    /**
     * 新增页面初始化
     * @author huajie <banhuajie@163.com>
     */
    public function add(){
        $model_id   =   I('get.model_id');
        $model      =   M('Model')->field('title,name,field_group')->find($model_id);
        $this->assign('model',$model);
        $this->assign('info', array('model_id'=>$model_id));
        $this->meta_title = '新增属性';
        $this->display('edit');
    }

    /**
     * 编辑页面初始化
     * @author huajie <banhuajie@163.com>
     */
    public function edit(){
        $id = I('get.id','');
        if(empty($id)){
            $this->error('参数不能为空！');
        }

        /*获取一条记录的详细数据*/
        $Model = M('Attribute');
        $data = $Model->field(true)->find($id);
        if(!$data){
            $this->error($Model->getError());
        }
        $model  =   M('Model')->field('title,name,field_group')->find($data['model_id']);
        $this->assign('model',$model);
        $this->assign('info', $data);
        $this->meta_title = '编辑属性';
        $this->display();
    }

    /**
     * 更新一条数据
     * @author huajie <banhuajie@163.com>
     */
    public function update(){
        $res = D('Attribute')->update();
        if(!$res){
            $this->error(D('Attribute')->getError());
        }else{
            $this->success($res['id']?'更新成功':'新增成功', Cookie('__forward__'));
        }
    }

    /**
     * 删除一条数据
     * @author huajie <banhuajie@163.com>
     */
    public function remove(){
        $id = I('id');
        empty($id) && $this->error('参数错误！');

        $Model = D('Attribute');

        $info = $Model->getById($id);
        empty($info) && $this->error('该字段不存在！');

        //删除属性数据
        $res = $Model->delete($id);

        //删除表字段
        $Model->deleteField($info);
        if(!$res){
            $this->error(D('Attribute')->getError());
        }else{
            //记录行为
            action_log('update_attribute', 'attribute', $id, UID);
            $this->success('删除成功', U('index','model_id='.$info['model_id']));
        }
    }
}
